import Vue from 'vue'
import VueRouter from 'vue-router'
import LayoutPage from '@/views/Layout.vue'
// import ArticlePage from '@/views/Article.vue'
// import LikePage from '@/views/Like.vue'
// import CollectPage from '@/views/Collect.vue'
// import DetailPage from '@/views/Detail.vue'
import { getToken } from '@/utils/storage'

Vue.use(VueRouter)

const routes = [
  // { path: '/register', component: RegisterPage },
  // 路由懒加载
  { path: '/register', component: () => import('@/views/Register.vue') },
  { path: '/login', component: () => import('@/views/Login.vue') },
  {
    path: '/',
    component: LayoutPage,
    children: [
      { path: '/article', component: () => import('@/views/Article.vue') },
      { path: '/like', component: () => import('@/views/Like.vue') },
      { path: '/collect', component: () => import('@/views/Collect.vue') },
      { path: '/user', component: () => import('@/views/User.vue') }
    ]
  },
  { path: '/detail', component: () => import('@/views/Detail.vue') }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/register', '/login']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
